package com.jkoss.wine.memberManagement.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.Constant;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.memberManagement.entity.Member;
import com.jkoss.wine.memberManagement.service.IMemberService;
import com.jkoss.wine.memberManagement.vo.MemberVo;
import com.jkoss.wine.merchantOrder.vo.MerchantOrderVo;
import com.jkoss.wine.shop.entity.Shop;
import com.jkoss.wine.shop.service.IShopService;
import com.jkoss.wine.shopOrder.service.ICardRechargeService;
import com.jkoss.wine.shopOrder.service.IOrdersService;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.entity.Role;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

/**
 * 前端控制器
 * 
 * @Author Supreme
 * @Version 1.0, 2018-10-04
 * @See
 * @Since com.jkoss.wine.memberManagement.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/memberManagement/member")
public class MemberController extends BaseController {

	@Autowired
	private IMemberService iMemberService;

	@Autowired
	private IShopService iShopService;
	@Autowired
	private ICardRechargeService iCardRechargeService;
	@Autowired
	private IOrdersService iOrdersService;

	/**
	 * 列表
	 * 
	 * @param memberName
	 * @param shopName
	 * @param memberState
	 * @param memberSex
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/memberManagement/member/list")
	public String list(String memberName, String shopName, Integer memberState, Integer memberSex, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		String string1 = request.getParameter("org2.shopName");
		String string2 = request.getParameter("org2.memberName");
		dwzPageBean.getCountResultMap().put("memberState", memberState);
		dwzPageBean.getCountResultMap().put("memberSex", memberSex);
		dwzPageBean.getCountResultMap().put("memberName", string2);
		dwzPageBean.getCountResultMap().put("shopName", string1);
		// Wrapper wrapper = Condition.create().ne("m.del", 1).like("m.member_state",
		// memberState).like("m.member_sex", memberSex).like("m.member_name",
		// string2).like("s.shop_name", string1);
		Wrapper wrapper = Condition.create().ne("m.del", 1).like("m.member_name", string2).like("s.shop_name", string1);

		if (!CommonUtil.isBlank(memberState)) {
			wrapper.eq("m.member_state", memberState);
		}
		if (!CommonUtil.isBlank(memberSex)) {
			wrapper.eq("m.member_sex", memberSex);
		}

		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("m.add_time", false);
		}

		Page resultPage = iMemberService.selectVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "memberManagement/member/list";
	}

	/**
	 * 去新增
	 * 
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/memberManagement/member/toInsert")
	public String toInsert(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().ne("del", 1);
		List shoplist = iShopService.selectList(wrapper);
		List<Member> memberlist = iMemberService.selectList(wrapper);
		map.put("shoplist", shoplist);
		map.put("memberlist", memberlist);
		return "memberManagement/member/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/memberManagement/member/toInsert")
	@ResponseBody
	public Object insert(@Valid Member member, HttpServletRequest request, HttpServletResponse response) {
		// 获取当前时间
		member.setAddTime(CommonUtil.getDate());
		// 获取当前操作人，当操作人为null时，表示自动录入
		MerchantEmployees employees = (MerchantEmployees) request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		member.setOperator(employees.getEmployeeId());
		// 是否删除：0-未删除、1-已删除、2-不能删除
		member.setDel(0);
		member.setIsSupper(1);
		if (iMemberService.insert(member)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/memberManagement/member/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().ne("del", 1);
		List shoplist = iShopService.selectList(wrapper);
		List<Member> memberlist = iMemberService.selectList(wrapper);
		map.put("shoplist", shoplist);
		map.put("memberlist", memberlist);
		map.put("record", iMemberService.selectById(id));
		return "memberManagement/member/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/memberManagement/member/toUpdate")
	@ResponseBody
	public Object update(@Valid Member member, HttpServletRequest request, HttpServletResponse response) {
		if (iMemberService.updateById(member)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/memberManagement/member/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		if (iMemberService.deleteBatchIds(Arrays.asList(id))) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 查询店铺名称
	 * 
	 * @param shopName
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getShopName")
	@ResponseBody
	public Object getShopName(String shopName, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("shop_name", shopName).ne("del", 1);
		List list = iShopService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 查询会员名称
	 * 
	 * @param memberName
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getMemberName")
	@ResponseBody
	public Object getMemberName(String memberName, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("member_name", memberName).ne("del", 1);
		List list = iMemberService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 导出Excel
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/export")
	@RequiresPermissions("/memberManagement/member/export")
	public String export(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create().ne("m.del", 1);
		// 根据用户id获取角色
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if ("运营主管".equals(role.getName())) {
				isDirector = false;
			}
		}
		if (isDirector) {
			wrapper.eq("maid", getUserId());
		}
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iMemberService.selectVoByPage(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "会员列表", ExcelType.XSSF);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, MemberVo.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "会员列表");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

	/**
	 * 会员卡充值列表
	 * 
	 * @param shopName
	 * @param startTime
	 * @param endTime
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/cardRechargeList")
	@RequiresPermissions("/memberManagement/member/cardRechargeList")
	public String cardRechargeList(String id, String shopName, String typeName, String startTime, String endTime, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		String string = request.getParameter("org2.shopName");
		// 查询会员卡类型
		String string2 = request.getParameter("org1.typeName");
		// 前端框架bean
		dwzPageBean.getCountResultMap().put("shopName", string);
		dwzPageBean.getCountResultMap().put("typeName", string2);

		dwzPageBean.getCountResultMap().put("startTime", startTime);
		dwzPageBean.getCountResultMap().put("endTime", endTime);
		dwzPageBean.getCountResultMap().put("id", id);
		Wrapper wrapper = Condition.create().isWhere(null).eq("m.member_id", id).like("s.shop_name", string).like("ct.type_name", string2);

		if (!CommonUtil.isBlank(startTime)) {
			// 大于等于开始时间
			wrapper.ge("cr.add_time", startTime);
		}
		if (!CommonUtil.isBlank(endTime)) {
			// 小于等于结束时间
			wrapper.le("cr.add_time", endTime);
		}

		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("cr.add_time", false);
		}

		Page resultPage = iCardRechargeService.selectVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "memberManagement/cardRecharge/list";
	}

	/**
	 * 列表
	 * 
	 * @param orderNum
	 * @param shopName
	 * @param startTime
	 * @param endTime
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/orderList")
	@RequiresPermissions("/memberManagement/member/orderList")
	public String orderList(String id, String orderNum, String shopName, String startTime, String endTime, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		String string1 = request.getParameter("org2.orderNum");
		String string2 = request.getParameter("org2.shopName");
		dwzPageBean.getCountResultMap().put("orderNum", string1);
		dwzPageBean.getCountResultMap().put("shopName", string2);
		dwzPageBean.getCountResultMap().put("startTime", startTime);
		dwzPageBean.getCountResultMap().put("endTime", endTime);
		dwzPageBean.getCountResultMap().put("id", id);
		Wrapper wrapper = Condition.create().eq("b.member_id", id).ne("a.del", 1).eq("a.order_type", 1).like("a.order_num", string1).like("s.shop_name", string2);

		if (!CommonUtil.isBlank(startTime)) {
			// 大于等于开始时间
			wrapper.ge("a.add_time", startTime);
		}
		if (!CommonUtil.isBlank(endTime)) {
			// 小于等于结束时间
			wrapper.le("a.add_time", endTime);
		}

		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("a.add_time", false);
		}

		Page resultPage = iOrdersService.selectVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "/memberManagement/orders/list";
	}
}
